#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    timer = new QTimer;//初始化
    picID=1;
    QImage img;//图片显示
    img.load("D:\\QT\\QtProject\\4_QTimer\\1.jpg");
    ui->label->setPixmap(QPixmap::fromImage(img));

    //定时器时间到，发出timeout信号
    connect(timer,&QTimer::timeout,this,&Widget::timeoutSlot);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    timer->start(TIMEOUT);
}

void Widget::on_stopButton_clicked()
{
    timer->stop();
}

void Widget::timeoutSlot()
{
    QString path("D:\\QT\\QtProject\\4_QTimer\\");
    path += QString::number(picID+1);
    path += ".jpg";

    QImage img; //图片显示
    img.load(path);
    ui->label->setPixmap(QPixmap::fromImage(img));
    picID = (++picID)%3;
}

void Widget::on_pushButton_2_clicked() //单次计时
{
    QTimer::singleShot(1000,this,SLOT(timeoutSlot()));
}
